package com.sljy.hzk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.TaskEntity;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.TaskDao;
import com.sljy.hzk.room.dao.UserDao;

import java.util.List;

public class TaskPersonAdapter   extends BaseAdapter {
    private  List<TaskEntity.PersonsBean> Datas;
    private Context mContext;
    UserDao userDao;
    TaskDao taskDao;

    public TaskPersonAdapter(List<TaskEntity.PersonsBean> datas, Context mContext) {
        Datas = datas;
        userDao = AppDataBase.getInstance().getUserDao();
        taskDao = AppDataBase.getInstance().getTaskDao();
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return Datas.size();
    }

    @Override
    public Object getItem(int position) {
        return Datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            //可以理解为从vlist获取view 之后把view返回给ListView
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_person,parent,false);
            holder.nameStr = (TextView)convertView.findViewById(R.id.name);
            holder.workStr = (TextView) convertView.findViewById(R.id.work);
            holder.phoneStr = (TextView) convertView.findViewById(R.id.phone);
            holder.stateBg = (ImageView) convertView.findViewById(R.id.stateBg);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        TaskEntity.PersonsBean d = Datas.get(position);
        holder.nameStr.setText(d.getName());
        holder.workStr.setText(d.getWorkType());
        holder.phoneStr.setText(d.getTel());
        String mName = UserInfo.getUserInfo().getName();
        if(mName.equals(d.getName()))
            holder.stateBg.setBackgroundResource(R.drawable.own);
        else
            holder.stateBg.setBackgroundResource(R.drawable.others);
        return convertView;
    }

    public final class ViewHolder {
        public TextView nameStr;
        public TextView workStr;
        public TextView phoneStr;
        public ImageView stateBg;
    }

}
